<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class SeckillglobalController extends Controller
{
	//秒杀商品列表
	public function getIndex(Request $request,$sid='')
	{

		if(empty($sid)){
			$cate = DB::table('seckillglobal')
			->orderBy('id','desc')
			->get();
		}else{
			$cate = DB::table('seckillglobal')
			->where('seckillid',$sid)
			->orderBy('id','desc')
			->get();
		}
		return view('admin.seckillglobal.index',['cate'=>$cate]);
	}

	//每日必看
	public function getMustsee(Request $request)
	{
		$cate = DB::table('seckillglobal')
		->where('mustsee','1')
		->orderBy('id','desc')
		->get();

		return view('admin.seckillglobal.mustsee',['cate'=>$cate]);
	}

	//加入每日必看
	public function getMuset($id)
	{
		$res = DB::table('seckillglobal')
		->where('id',$id)
		->update(['mustsee' => '1']);
		if($res){
			return back()->with('success','添加成功');
		}else{
			return back()->with('error','添加失败');
		}
	}
	//取消每日必看
	public function getMymuset($id)
	{
		$res = DB::table('seckillglobal')
		->where('id',$id)
		->update(['mustsee' => '0']);
		if($res){
			return back()->with('success','取消成功');
		}else{
			return back()->with('error','取消失败');
		}
	}

  //添加
	public function getAdd($sid='')
	{
		$cate = self::getCatesByPid(0);
		$arr = self::brand(0);

		$cates = self::Cates();

		$effect = DB::table('effect')->get();

		if(empty($sid)){
			return view('admin.seckillglobal.add',['sid'=>'','cate'=>$cate,'arr'=>$arr,'cates'=>$cates,'effect'=>$effect]);
		}else{
			return view('admin.seckillglobal.add',['sid'=>$sid,'cate'=>$cate,'arr'=>$arr,'cates'=>$cates,'effect'=>$effect]);
		}
	}
	//执行添加
	public function postInsert(Request $request)
	{
		$data = $request->except(['pic','file','_token','effect']);
		$data['time'] = time();
		$data['img'] = $this -> upload($request,'pic');
		$data['myimg'] = $this -> upload($request,'file');
		$res['gid'] = DB::table('seckillglobal')->insertGetId($data);
		if($res){
			$effect = $request->input('effect');
			foreach ($effect as $k => $v) {
				$res['effectid'] = $v;
				DB::table('goodseffect')->insert($res);
			}
			return redirect('/admin/seckillglobal/index')->with('success','添加成功');
		}else{
			return badk()->with('error','添加失败');
		}
	}
	//删除
	public function getDelete($id)
	{
		
		$res = DB::table('seckillglobal')->where('id',$id)->delete();

		if($res){
			return back()->with('success','删除成功');
		}else{
			return back()->with('error','删除失败');
		}
	}
	//修改
	public function getEdit($id)
	{
		$cates = DB::table('seckill')->where('id',$id)->first();
		return view('admin.seckill.edit',['cates'=>$cates]);
	}
	//执行修改
	public function postUpdate(Request $request)
	{
		$id = $request->input('id');
		$arr = $request->except(['pic','_token','id']);;
		if($arr['name'] == ''){
			return back()->withInput();
		}
		$res = DB::table('seckill')->where('id',$id)->update($arr);
		if(!$res){
			return back()->with('error','修改失败');
		}

		$obj = new FileUpload();
		$obj->upload('pic');
		$r = $obj->getFileName();
		$data['id'] = $id;
		DB::table('seckimg')->where('id',$id)->delete();
		foreach($r as $k => $v){
			$data['imginfo'] = $v;
			$srec = DB::table('seckimg')->insert($data);
		}
		if($srec){
			return back()->with('success','修改成功');
		}else{
			return back()->with('error','修改失败');
		}
	}

	public function upload($request,$name)
	{
		if($request->hasFile($name)){
			$n = md5(rand(1,9999).time());
			$suffix = $request->file($name)->getClientOriginalExtension();
			$arr = array('jpg','png','jpeg','gif');
			if(!in_array($suffix,$arr)){
				die('上传文件不符合要求');
			}
			$request->file($name)->move('./uploads',$n.'.'.$suffix);
			return '/uploads/'.$n.'.'.$suffix;
		}
   }

   	//根据pid获取该分类下的所有子分类
     public static function getCatesByPid($gid){
         //获取$pid下的所有子分类
         $res = DB::table('goodsort')->where('gid',$gid)->get();
         $data = [];
         //遍历
         foreach ($res as $k=>$v){
             $v->sub = self::getCatesByPid($v->id);
             $data[] = $v;
         }
         return $data;
     }
     public static function getCates($gid){
         //获取$pid下的所有子分类
         $res = DB::table('brand')->where('gid',$gid)->get();
         $data = [];
         //遍历
         foreach ($res as $k=>$v){
             $v->sub = self::getCates($v->id);
             $data[] = $v;
         }
         return $data;
     }

     //处理分类结果
    public static function Cates()
	{
		$res = DB::table('goodsort')
		    ->select(DB::raw("*,concat(path,'-',id) as paths"))
		    ->orderBy('paths')
		    ->get();
		//处理数据
		foreach($res as $k=>$v){
			$arr = explode('-',$v->path);
			$num = count($arr)-1;
			$res[$k]->goodsname = str_repeat('|--',$num).$v->goodsname;
		}
		return $res;
	}
	public static function brand()
	{
		$res = DB::table('brand')
		    ->select(DB::raw("*,concat(path,'-',id) as paths"))
		    ->orderBy('paths')
		    ->get();
		//处理数据
		foreach($res as $k=>$v){
			$arr = explode('-',$v->path);
			$num = count($arr)-1;
			$res[$k]->name = str_repeat('|--',$num).$v->name;
		}
		return $res;
	}
}
